package com.ihighest.app.infox.adapter;

import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ihighest.app.common.kit.AppKit;
import com.ihighest.app.common.kit.StrKit;
import com.ihighest.app.infox.R;
import com.nostra13.universalimageloader.core.DisplayImageOptions;

/**
 * 我收藏的适配器
 * 
 * @author lkx Created at 2015-10-13 上午10:39:15
 * @version 1.0
 */
public class MyCollectionAdapter extends BaseAdapter {
	JSONArray array;
	Activity activity;
	LayoutInflater inflater = null;
	// protected ImageLoader imageLoader = ImageLoader.getInstance();
	DisplayImageOptions options;

	public MyCollectionAdapter(Activity activity, JSONArray array) {
		this.activity = activity;
		this.array = array;
		inflater = LayoutInflater.from(activity);

	}

	public MyCollectionAdapter(Activity activity) {
		this.activity = activity;
		inflater = LayoutInflater.from(activity);

	}

	@Override
	public int getCount() {

		// TODO Auto-generated method stub
		return array == null ? 0 : array.length();
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = inflater.inflate(R.layout.infox_activity_mcollection_list_item, null);
			holder.comment_count = (TextView) convertView.findViewById(R.id.newsCommentCount);
			holder.item_source = (TextView) convertView.findViewById(R.id.newsProvenance);
			holder.publish_time = (TextView) convertView.findViewById(R.id.newsDate);
			holder.normalItem_image = (ImageView) convertView.findViewById(R.id.mCollectionListItemNameSmallImg);
			holder.item_title = (TextView) convertView.findViewById(R.id.mCollectionListItemName);
			holder.item_bigImage_layout = (RelativeLayout) convertView.findViewById(R.id.newBigImgLayout);
			holder.normalItem_layout = (RelativeLayout) convertView.findViewById(R.id.newSmallImgLayout);
			holder.item_bigImage_title = (TextView) convertView.findViewById(R.id.mCollectionListBigItemName);
			holder.item_bigImage = (ImageView) convertView.findViewById(R.id.mCollectionListBigImg);
			// holder.list_item_local = (TextView)
			// convertView.findViewById(R.id.list_item_local);
			holder.noImgLayout = (RelativeLayout) convertView.findViewById(R.id.noImgLayout);
			holder.noImgTitle = (TextView) convertView.findViewById(R.id.noImgLayoutTitle);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		// 设置字体大小
		// holder.noImgTitle.setTextSize(SharedPreferencesKit.getFloat(activity,
		// Const.SP_TEXTSIZE, Const.noramlSize));
		// holder.item_title.setTextSize(SharedPreferencesKit.getFloat(activity,
		// Const.SP_TEXTSIZE, Const.noramlSize));
		// holder.item_bigImage_title.setTextSize(SharedPreferencesKit.getFloat(activity,
		// Const.SP_TEXTSIZE, Const.noramlSize));
		try {
			// if
			// (TextUtils.isEmpty(array.getJSONObject(position).getString("image")))
			// {
			// 如果没有图片
			holder.item_bigImage_layout.setVisibility(View.GONE);
			holder.normalItem_layout.setVisibility(View.GONE);
			holder.noImgLayout.setVisibility(View.VISIBLE);
			holder.noImgTitle.setText(array.getJSONObject(position).getString("title"));

			// } else {
			// 如果有图片
			// holder.item_bigImage_layout.setVisibility(View.GONE);
			// holder.normalItem_layout.setVisibility(View.VISIBLE);
			// holder.noImgLayout.setVisibility(View.GONE);
			// holder.item_title.setText(array.getJSONObject(position).getString("title"));
			// Resources resources = activity.getResources();
			// Drawable drawable =
			// resources.getDrawable(R.drawable.infox_img_loadrong);
			// DisplayImageOptions defaultOptions = new
			// DisplayImageOptions.Builder().showImageForEmptyUri(drawable)
			// .showImageOnFail(drawable).showImageOnLoading(drawable).cacheInMemory(true).cacheOnDisc(true).build();
			// ImageLoader.getInstance().displayImage(array.getJSONObject(position).getString("image"),
			// holder.normalItem_image,
			// defaultOptions);
			// AppKit.SetNormalImg(array.getJSONObject(position).getString("image"),
			// holder.normalItem_image);

			// }
			// if (array.getJSONObject(position).has("sourceid")) {
			// if
			// (StrKit.isBlank(array.getJSONObject(position).getString("sourceid")))
			// {
			// holder.item_source.setText("原创");
			// }
			// } else {
			// holder.item_source.setText(array.getJSONObject(position).getString("source"));
			// }
			// holder.comment_count.setText(array.getJSONObject(position).getString("comments")
			// + "条评论");
			// holder.publish_time.setText(AppKit.timeToBeforeStr(array.getJSONObject(position).getLong("time")));
			// if
			// (TextUtils.isEmpty(array.getJSONObject(position).getString("tag")))
			// {
			// holder.list_item_local.setVisibility(View.GONE);
			// } else {
			// holder.list_item_local.setVisibility(View.VISIBLE);
			// if
			// (array.getJSONObject(position).getString("tag").equals("_HOT")) {
			// // 表示该新闻是热门新闻
			// holder.list_item_local.setBackgroundResource(R.drawable.infox_information_hotbg);
			// holder.list_item_local.setTextColor(activity.getResources().getColor(R.color.hotItemcolor));
			// holder.list_item_local.setText("热");
			// } else if
			// (array.getJSONObject(position).getString("tag").equals("_SUB")) {
			// // 表示该新闻是订阅新闻
			// holder.list_item_local.setBackgroundResource(R.drawable.infox_information_dingyuebg);
			// holder.list_item_local.setTextColor(activity.getResources().getColor(R.color.subscriptionItemcolor));
			// holder.list_item_local.setText("订阅");
			// } else if
			// (array.getJSONObject(position).getString("tag").equals("_PRO")) {
			// // 表示该新闻是推广新闻
			// holder.list_item_local.setBackgroundResource(R.drawable.infox_information_popularizebg);
			// holder.list_item_local.setTextColor(activity.getResources().getColor(R.color.popularizeItemcolor));
			// holder.list_item_local.setText("推广");
			// } else if
			// (array.getJSONObject(position).getString("tag").equals("_REM")) {
			// // 表示该新闻是推荐新闻
			// holder.list_item_local.setBackgroundResource(R.drawable.infox_information_recommendbg);
			// holder.list_item_local.setTextColor(activity.getResources().getColor(R.color.recommendItemcolor));
			// holder.list_item_local.setText("荐");
			// } else {
			// holder.list_item_local.setBackgroundResource(R.drawable.infox_information_plantbg);
			// holder.list_item_local.setTextColor(activity.getResources().getColor(R.color.orange_yellow));
			// holder.list_item_local.setText(array.getJSONObject(position).getString("tag"));//
			// 测试
			// }
			// }
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return convertView;
	}

	static class ViewHolder {
		RelativeLayout normalItem_layout;
		// title
		TextView item_title;
		// 新闻来源
		TextView item_source;
		// 类似推广之类的标签
		// TextView list_item_local;
		// 评论数量
		TextView comment_count;
		// 发布时间
		TextView publish_time;
		// 小图片
		ImageView normalItem_image;
		// 大图片布局
		RelativeLayout item_bigImage_layout;
		// title
		TextView item_bigImage_title;
		ImageView item_bigImage;
		// 没有图片布局
		RelativeLayout noImgLayout;
		TextView noImgTitle;
	}

	/**
	 * 替换当前数据源数据
	 * 
	 * @param moreArry
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:23
	 */
	public void setData(JSONArray jsonArray) {
		array = jsonArray;
		notifyDataSetChanged();
	}

	/**
	 * 向已有数据源中追加数据
	 * 
	 * @param jsonArray
	 * 
	 * @author lkx Created at 2015-10-20 下午8:49:00
	 */
	public void appendData(JSONArray jsonArray) {
		try {
			array = AppKit.joinJSONArray(array, jsonArray);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		notifyDataSetChanged();
	}
}
